package leecode.q503;

import java.util.Arrays;
import java.util.Stack;

public class Solution {
    public void solve() {

    }

    public int[] nextGreaterElements(int[] nums) {
        int length = nums.length;
        int[] res = new int[length];
        Stack<Integer> stack = new Stack<>();
        Arrays.fill(res, -1);
        for (int i = 0; i < length * 2; i++) {
            while (!stack.empty() && nums[i % length] > nums[stack.peek()]) {
                res[stack.peek()] = nums[i % length];
                stack.pop();
            }
            stack.push(i % length);
        }

        return res;
    }
}
